<?php
            
use app\model\Theme; 


class Music_Page extends PluginWebPage
{
    function main($method)
    {
        $method = 'music_main';
        $do = 'weike';
        global $_G,$_P,$_S,$_C;
        $system = $_S;
        $config = $_C;
        $themeid = intval($_P['themeid']);
        if($themeid <= 0){
            $themeid = Theme::getDefaultThemenew();
        }
        $themelist = Theme::getAllTheme();
        $page_index = max(1,intval($_P['page']));
        $page_size = 50;
        $page_start = ($page_index-1)*$page_size;
        $search_data = isset($_P['search_data'])?$_P['search_data']:'';
        if ($search_data){
            $search_data = '%'.$search_data.'%';
        }
        $list = WeikeModel::getMusicList($themeid,false,false,$search_data,$page_start,$page_size);
        $count = WeikeModel::getMusicList($themeid,false,false, $search_data,0,0);
        $total = $page_total = $count['count_num'];
        $pager = pagination($page_total,$page_index,$page_size);
        $return_clicent = get_defined_vars();
return $this->view('/weike/music/main', $return_clicent);
    }

    function addpage(){
        $method = 'music_add';
        $do = 'weike';
        global $_G,$_P,$_S,$_C;
        $system = $_S;
        $config = $_C;
        $themeid = intval($_P['themeid']);
        if($themeid <= 0){
            $themeid = Theme::getDefaultThemenew();
        }
        $music_id = intval($_P['music_id'])?:0;
        $data = [];
        if($music_id){
            $data = WeikeModel::getMusicInfoById($music_id,$themeid);
        }
        $musiclist = db_select ( "SELECT * FROM " . tablename ( "00musiclist" ) . " ORDER BY id");
        $return_clicent = get_defined_vars();
return $this->view('/weike/music/edit', $return_clicent);
    }

    function addinfo(){
        global $_G,$_P,$_S,$_C;
        $system = $_S;
        $config = $_C;
        $themeid = intval($_P['themeid']);
        if($themeid <= 0){
            $themeid = Theme::getDefaultThemenew();
        }
        if (!$themeid){
            show_json(0,'活动入口不正确');
            return;
        }
        $name = trim($_P['name'])?:'';
        if(empty($name)){
            show_json(0,'请输入音乐名称');
            return;
        }
        $url = tomedia(trim($_P['music_url']));
        if(empty($url)){
            show_json(0,'请选择或上传音乐');
            return;
        }
        $status = $_P['status']?:0;
        $music_id = $_P['music_id']?:0;
        //拼接数组
        $data = [
            'themeid'=>$themeid,
            'name'=>$name,
            'url'=>$url,
            'status'=>$status,
        ];
        if($music_id){
            //编辑
            //根据ID查找信息
            $info = WeikeModel::getMusicInfoById($music_id,$themeid);
            if(empty($info)){
                show_json(0,'未找到音乐信息');
                return;
            }
            //修改数据库字段
            $res = WeikeModel::editMusicDataByWhere(['id'=>$music_id],$data);
            if (!$res){
                show_json(0,['message'=>'操作失败']);
                return;
            }else{
                show_json(1,['message'=>'操作成功','music_id'=>$music_id]);
                return;
            }
        }else{
            //存入数据库
            $res = WeikeModel::insertMusicData($data);
            if (!$res){
                show_json(0,['message'=>'操作失败']);
                return;
            }else{
                show_json(1,['message'=>'操作成功','music_id'=>$res]);
                return;
            }
        }
    }
}